import re


def parse_message_xuji_698(str_message):
    """
    解析5002召测协议，支持许继698-非浙江、华立安徽698、南瑞698、三晖698、威盛698、炬华698
    :param str_message:
    :return:
    """
    str_message = " ".join(re.findall(".{2}", str_message))
    # 获取互感器地址
    transformer_address = " ".join(list(reversed(str_message[5 * 3:11 * 3 - 1].split(" "))))
    #  去除字符串空格
    transformer_address = transformer_address.replace(" ", "")
    # print(transformer_address)

    # 获取时间
    str_time = str_message[39 * 3:47 * 3 - 1]
    # print(str_time)
    assert int(str_time[:2], 16) == 28, "时间类型非28"
    year = str(int(str_time[3:8].replace(" ", ""), 16))
    month = str(int(str_time[9:12], 16)).zfill(2)
    day = str(int(str_time[12:15], 16)).zfill(2)
    hour = str(int(str_time[15:18], 16)).zfill(2)
    minute = str(int(str_time[18:21], 16)).zfill(2)
    second = str(int(str_time[21:24], 16)).zfill(2)
    collect_time = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second
    # print(collect_time)

    # 获取采集频率
    str_frequency = str_message[49 * 3:54 * 3 - 1]
    assert int(str_frequency[:2], 16) == 6, "频率类型非6"
    frequency = int(str_frequency[2:].replace(" ", ""), 16)
    # print(frequency)

    # 获取采集幅值
    str_amplitude = str_message[54 * 3:63 * 3 - 1]
    assert int(str_amplitude[:2], 16) == 21, "幅值类型非21"
    amplitude = int(str_amplitude[2:].replace(" ", ""), 16)
    # print(amplitude)

    # 获取采集相位
    str_phase = str_message[63 * 3:68 * 3 - 1]
    # print(str_phase)
    assert int(str_phase[:2], 16) == 5, "相位类型非5"
    if str_phase[3:5] == "FF" or str_phase[3:5] == "ff":
        phase = int(str_phase[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_phase[3:5] == "00":
        phase = int(str_phase[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_phase[3:5])
    # print(phase)

    # 获取比差
    str_ratio = str_message[74 * 3:79 * 3 - 1]
    # print(str_ratio)
    # print(int(str_ratio[:2], 16))
    assert int(str_ratio[:2], 16) == 5, "比差类型非5"
    if str_ratio[3:5] == "FF" or str_ratio[3:5] == "ff":
        ratio = int(str_ratio[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_ratio[3:5] == "00":
        ratio = int(str_ratio[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_ratio[3:5])
    # print(ratio)

    # 获取角差
    str_angle = str_message[79 * 3:84 * 3 - 1]
    # print(str_angle)
    assert int(str_angle[:2], 16) == 5, "角差类型非5"
    if str_angle[3:5] == "FF" or str_angle[3:5] == "ff":
        angle = int(str_angle[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_angle[3:5] == "00":
        angle = int(str_angle[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_angle[3:5])
    # print(angle)

    # 获取状态
    str_status = str_message[84 * 3:88 * 3 - 1]
    # print(str_status)
    assert int(str_status[:2], 16) == 4, "状态类型非4"
    # 状态码16进制转2进制，截取倒数第六位到倒数第9位，再转10进制
    status = int(bin(int(str_status[6:].replace(" ", ""), 16))[2:].zfill(16)[-9:-5], 2)
    # print(status)
    return transformer_address, collect_time, frequency, amplitude, phase, ratio, angle, status


def parse_message_xuji_698_2070(str_message):
    """
    解析许继698（浙江）协议 带有2070报文
    :param str_message:
    :return:
    """
    str_message = " ".join(re.findall(".{2}", str_message))
    # 获取互感器地址
    transformer_address = " ".join(list(reversed(str_message[5 * 3:11 * 3 - 1].split(" "))))
    #  去除字符串空格
    transformer_address = transformer_address.replace(" ", "")
    # print(transformer_address)

    # 获取时间
    str_time = str_message[44 * 3:52 * 3 - 1]
    assert int(str_time[:2], 16) == 28, "时间类型非28"
    year = str(int(str_time[3:8].replace(" ", ""), 16))
    month = str(int(str_time[9:12], 16)).zfill(2)
    day = str(int(str_time[12:15], 16)).zfill(2)
    hour = str(int(str_time[15:18], 16)).zfill(2)
    minute = str(int(str_time[18:21], 16)).zfill(2)
    second = str(int(str_time[21:24], 16)).zfill(2)
    collect_time = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second
    # print(collect_time)

    # 获取采集频率
    str_frequency = str_message[54 * 3:59 * 3 - 1]
    assert int(str_frequency[:2], 16) == 6, "频率类型非6"
    frequency = int(str_frequency[2:].replace(" ", ""), 16)
    # print(frequency)

    # 获取采集幅值
    str_amplitude = str_message[59 * 3:68 * 3 - 1]
    assert int(str_amplitude[:2], 16) == 21, "幅值类型非21"
    amplitude = int(str_amplitude[2:].replace(" ", ""), 16)
    # print(amplitude)

    # 获取采集相位
    str_phase = str_message[68 * 3:73 * 3 - 1]
    # print(str_phase)
    assert int(str_phase[:2], 16) == 5, "相位类型非5"
    if str_phase[3:5] == "FF" or str_phase[3:5] == "ff":
        phase = int(str_phase[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_phase[3:5] == "00":
        phase = int(str_phase[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_phase[3:5])
    # print(phase)

    # 获取比差
    str_ratio = str_message[79 * 3:84 * 3 - 1]
    # print(str_ratio)
    assert int(str_ratio[:2], 16) == 5, "比差类型非5"
    if str_ratio[3:5] == "FF" or str_ratio[3:5] == "ff":
        ratio = int(str_ratio[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_ratio[3:5] == "00":
        ratio = int(str_ratio[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_ratio[3:5])
    # print(ratio)

    # 获取角差
    str_angle = str_message[84 * 3:89 * 3 - 1]
    # print(str_angle)
    assert int(str_angle[:2], 16) == 5, "角差类型非5"
    if str_angle[3:5] == "FF" or str_angle[3:5] == "ff":
        angle = int(str_angle[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_angle[3:5] == "00":
        angle = int(str_angle[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_angle[3:5])
    # print(angle)

    # 获取状态
    str_status = str_message[89 * 3:93 * 3 - 1]
    # print(str_status)
    assert int(str_status[:2], 16) == 4, "状态类型非4"
    status = int(str_status[6:].replace(" ", ""), 16)
    # print(status)

    # 获取比差
    str_round_ratio = str_message[97 * 3:102 * 3 - 1]
    # print(str_ratio)
    assert int(str_round_ratio[:2], 16) == 5, "比差类型非5"
    if str_round_ratio[3:5] == "FF" or str_round_ratio[3:5] == "ff":
        round_ratio = int(str_round_ratio[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_round_ratio[3:5] == "00":
        round_ratio = int(str_round_ratio[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_round_ratio[3:5])
    # print(round_ratio)

    # 获取角差
    str_round_angle = str_message[102 * 3:107 * 3 - 1]
    # print(str_round_angle)
    assert int(str_round_angle[:2], 16) == 5, "角差类型非5"
    if str_round_angle[3:5] == "FF" or str_round_angle[3:5] == "ff":
        round_angle = int(str_round_angle[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_round_angle[3:5] == "00":
        round_angle = int(str_round_angle[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_round_angle[3:5])
    # print(round_angle)

    # 获取标准比差
    str_stand_ratio = str_message[107 * 3:112 * 3 - 1]
    # print(str_stand_ratio)
    assert int(str_stand_ratio[:2], 16) == 5, "比差类型非5"
    if str_stand_ratio[3:5] == "FF" or str_stand_ratio[3:5] == "ff":
        stand_ratio = int(str_stand_ratio[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_stand_ratio[3:5] == "00":
        stand_ratio = int(str_stand_ratio[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_stand_ratio[3:5])
    # print(stand_ratio)

    # 获取标准角差
    str_stand_angle = str_message[112 * 3:117 * 3 - 1]
    # print(str_stand_angle)
    assert int(str_stand_angle[:2], 16) == 5, "角差类型非5"
    if str_stand_angle[3:5] == "FF" or str_stand_angle[3:5] == "ff":
        stand_angle = int(str_stand_angle[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_stand_angle[3:5] == "00":
        stand_angle = int(str_stand_angle[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_stand_angle[3:5])

    # 获取标准状态
    str_stand_status = str_message[117 * 3:121 * 3 - 1]
    # print(str_stand_status)
    assert int(str_stand_status[:2], 16) == 4, "状态类型非4"
    stand_status = int(str_stand_status[6:].replace(" ", ""), 16)
    # print(stand_status)

    # print(stand_angle)
    return transformer_address, collect_time, frequency, amplitude, phase, ratio, angle, status, round_ratio, round_angle, \
        stand_ratio, stand_angle, stand_status

# 华立698-山西（2280）
def parse_message_huali_698_sample(str_message):
    """
    解析华立698协议采样
    :param str_message:
    :return:
    """
    str_message = " ".join(re.findall(".{2}", str_message))
    # print(str_message)
    # 获取互感器地址
    transformer_address = " ".join(list(reversed(str_message[5 * 3:11 * 3 - 1].split(" "))))
    #  去除字符串空格
    transformer_address = transformer_address.replace(" ", "")
    # print(transformer_address)

    # 获取时间
    str_time = str_message[24 * 3:32 * 3 - 1]
    assert int(str_time[:2], 16) == 28, "时间类型非28"
    year = str(int(str_time[3:8].replace(" ", ""), 16))
    month = str(int(str_time[9:12], 16)).zfill(2)
    day = str(int(str_time[12:15], 16)).zfill(2)
    hour = str(int(str_time[15:18], 16)).zfill(2)
    minute = str(int(str_time[18:21], 16)).zfill(2)
    second = str(int(str_time[21:24], 16)).zfill(2)
    collect_time = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second
    # print(collect_time)

    # 获取采集频率
    str_frequency_a = str_message[60 * 3:65 * 3 - 1]
    # print(str_frequency_a)
    assert int(str_frequency_a[:2], 16) == 6, "频率类型非6"
    frequency_a = int(str_frequency_a[2:].replace(" ", ""), 16)
    # print(frequency_a)

    # 获取采集频率
    str_frequency_b = str_message[65 * 3:70 * 3 - 1]
    # print(str_frequency_b)
    assert int(str_frequency_b[:2], 16) == 6, "频率类型非6"
    frequency_b = int(str_frequency_b[2:].replace(" ", ""), 16)
    # print(frequency_b)

    # 获取采集频率
    str_frequency_c = str_message[70 * 3:75 * 3 - 1]
    # print(str_frequency_c)
    assert int(str_frequency_c[:2], 16) == 6, "频率类型非6"
    frequency_c = int(str_frequency_c[2:].replace(" ", ""), 16)
    # print(frequency_c)

    # 获取采集幅值
    str_amplitude_a = str_message[75 * 3:84 * 3 - 1]
    # print(str_amplitude)
    assert int(str_amplitude_a[:2], 16) == 21, "幅值类型非21"
    amplitude_a = int(str_amplitude_a[2:].replace(" ", ""), 16)
    # print(amplitude_a)

    # 获取采集幅值
    str_amplitude_b = str_message[84 * 3:93 * 3 - 1]
    # print(str_amplitude)
    assert int(str_amplitude_b[:2], 16) == 21, "幅值类型非21"
    amplitude_b = int(str_amplitude_b[2:].replace(" ", ""), 16)
    # print(amplitude_b)

    # 获取采集幅值
    str_amplitude_c = str_message[93 * 3:102 * 3 - 1]
    # print(str_amplitude)
    assert int(str_amplitude_c[:2], 16) == 21, "幅值类型非21"
    amplitude_c = int(str_amplitude_c[2:].replace(" ", ""), 16)
    # print(amplitude_c)

    # 获取采集相位
    str_phase_a = str_message[102 * 3:107 * 3 - 1]
    # print(str_phase_a)
    assert int(str_phase_a[:2], 16) == 5, "相位类型非5"
    if str_phase_a[3:5] == "FF" or str_phase_a[3:5] == "ff":
        phase_a = int(str_phase_a[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_phase_a[3:5] == "00":
        phase_a = int(str_phase_a[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_phase_a[3:5])
    # print(phase_a)

    # 获取采集相位
    str_phase_b = str_message[107 * 3:112 * 3 - 1]
    # print(str_phase_b)
    assert int(str_phase_b[:2], 16) == 5, "相位类型非5"
    if str_phase_b[3:5] == "FF" or str_phase_b[3:5] == "ff":
        phase_b = int(str_phase_b[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_phase_b[3:5] == "00":
        phase_b = int(str_phase_b[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_phase_b[3:5])
    # print(phase_b)

    # 获取采集相位
    str_phase_c = str_message[112 * 3:117 * 3 - 1]
    # print(str_phase_c)
    assert int(str_phase_c[:2], 16) == 5, "相位类型非5"
    if str_phase_c[3:5] == "FF" or str_phase_c[3:5] == "ff":
        phase_c = int(str_phase_c[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_phase_c[3:5] == "00":
        phase_c = int(str_phase_c[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_phase_c[3:5])
    # print(phase_c)
    return transformer_address, collect_time, frequency_a, amplitude_a, phase_a, \
        frequency_b, amplitude_b, phase_b, frequency_c, amplitude_c, phase_c


def parse_message_huali_698_calculate(str_message):
    """
    解析华立698协议计算
    :param str_message:
    :return:
    """
    str_message = " ".join(re.findall(".{2}", str_message))
    # print(str_message)
    # 获取互感器地址
    transformer_address = " ".join(list(reversed(str_message[5 * 3:11 * 3 - 1].split(" "))))
    #  去除字符串空格
    transformer_address = transformer_address.replace(" ", "")
    # print(transformer_address)

    # 获取时间
    str_time = str_message[24 * 3:32 * 3 - 1]
    assert int(str_time[:2], 16) == 28, "时间类型非28"
    year = str(int(str_time[3:8].replace(" ", ""), 16))
    month = str(int(str_time[9:12], 16)).zfill(2)
    day = str(int(str_time[12:15], 16)).zfill(2)
    hour = str(int(str_time[15:18], 16)).zfill(2)
    minute = str(int(str_time[18:21], 16)).zfill(2)
    second = str(int(str_time[21:24], 16)).zfill(2)
    collect_time = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second
    # print(collect_time)

    # 获取比差
    str_ratio_a = str_message[60 * 3:69 * 3 - 1]
    # print(str_ratio_a)
    assert int(str_ratio_a[:2], 16) == 20, "比差类型非20"
    if str_ratio_a[3:5] == "FF" or str_ratio_a[3:5] == "ff":
        ratio_a = int(str_ratio_a[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_ratio_a[3:5] == "00":
        ratio_a = int(str_ratio_a[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_ratio_a[3:5])
    # print(ratio_a)

    # 获取比差
    str_ratio_b = str_message[69 * 3:78 * 3 - 1]
    # print(str_ratio_b)
    assert int(str_ratio_b[:2], 16) == 20, "比差类型非20"
    if str_ratio_b[3:5] == "FF" or str_ratio_b[3:5] == "ff":
        ratio_b = int(str_ratio_b[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_ratio_b[3:5] == "00":
        ratio_b = int(str_ratio_b[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_ratio_b[3:5])
    # print(ratio_b)

    # 获取比差
    str_ratio_c = str_message[78 * 3:87 * 3 - 1]
    # print(str_ratio_c)
    assert int(str_ratio_c[:2], 16) == 20, "比差类型非20"
    if str_ratio_c[3:5] == "FF" or str_ratio_c[3:5] == "ff":
        ratio_c = int(str_ratio_c[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_ratio_c[3:5] == "00":
        ratio_c = int(str_ratio_c[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_ratio_c[3:5])
    # print(ratio_c)

    # 获取角差
    str_angle_a = str_message[87 * 3:96 * 3 - 1]
    # print(str_angle)
    assert int(str_angle_a[:2], 16) == 20, "角差类型非20"
    if str_angle_a[3:5] == "FF" or str_angle_a[3:5] == "ff":
        angle_a = int(str_angle_a[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_angle_a[3:5] == "00":
        angle_a = int(str_angle_a[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_angle_a[3:5])
    # print(angle_a)

    # 获取角差
    str_angle_b = str_message[96 * 3:105 * 3 - 1]
    # print(str_angle)
    assert int(str_angle_b[:2], 16) == 20, "角差类型非20"
    if str_angle_b[3:5] == "FF" or str_angle_b[3:5] == "ff":
        angle_b = int(str_angle_b[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_angle_b[3:5] == "00":
        angle_b = int(str_angle_b[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_angle_b[3:5])
    # print(angle_b)

    # 获取角差
    str_angle_c = str_message[105 * 3:114 * 3 - 1]
    # print(str_angle)
    assert int(str_angle_c[:2], 16) == 20, "角差类型非20"
    if str_angle_c[3:5] == "FF" or str_angle_c[3:5] == "ff":
        angle_c = int(str_angle_c[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_angle_c[3:5] == "00":
        angle_c = int(str_angle_c[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_angle_c[3:5])
    # print(angle_c)
    return transformer_address, collect_time, ratio_a, ratio_b, ratio_c, angle_a, angle_b, angle_c


def deal_645_message(start_index, end_index, str_message, coefficient):
    """
    处理威胜645报文，每两位减去0x33
    :param start_index: 
    :param end_index: 
    :param str_message: 
    :return: 
    """
    str_data = str_message[start_index:end_index]
    list_data = [hex(int("0x" + i, 16) - 0x33)[2:].zfill(2) for i in str_data.split(" ")]
    data = "".join(list(reversed(list_data)))

    if data[0] == "0":
        flag = 1
    elif data[0] == "8":
        flag = -1
    else:
        assert False, "首字母不正确:{}".format(data)
    convert_data = flag * int(data[1:]) / coefficient
    return convert_data


def parse_message_weisheng_645_sample(str_message):
    """
    威胜645解析采样数据
    :param str_message: 
    :return: 
    """
    str_message = " ".join(re.findall(".{2}", str_message))
    # print(str_message)

    # 获取互感器地址
    transformer_address = " ".join(list(reversed(str_message[1 * 3:7 * 3 - 1].split(" ")))).replace(" ", "")
    # print(transformer_address)

    # 获取时间
    str_time = str_message[16 * 3:21 * 3 - 1]
    list_time = [hex(int("0x" + i, 16) - 0x33)[2:].zfill(2) for i in str_time.split(" ")]
    collect_time = "20" + list_time[4] + "-" + list_time[3] + "-" + list_time[2] + " " + list_time[1] + ":" + list_time[
        0] + ":00"
    # print(collect_time)

    # 获取a相频率
    convert_a_frequency = deal_645_message(21 * 3, 26 * 3 - 1, str_message, 10000)
    # 获取a相幅值
    convert_a_amplitude = deal_645_message(26 * 3, 31 * 3 - 1, str_message, 10000)
    # 获取a相相位
    convert_a_phase = deal_645_message(41 * 3, 45 * 3 - 1, str_message, 100)
    # print(convert_a_frequency, convert_a_amplitude, convert_a_phase)

    # 获取b相频率
    convert_b_frequency = deal_645_message(53 * 3, 58 * 3 - 1, str_message, 10000)
    # 获取a相幅值
    convert_b_amplitude = deal_645_message(58 * 3, 63 * 3 - 1, str_message, 10000)
    # 获取a相相位
    convert_b_phase = deal_645_message(73 * 3, 77 * 3 - 1, str_message, 100)
    # print(convert_b_frequency, convert_b_amplitude, convert_b_phase)

    # 获取c相频率
    convert_c_frequency = deal_645_message(85 * 3, 90 * 3 - 1, str_message, 10000)
    # 获取a相幅值
    convert_c_amplitude = deal_645_message(90 * 3, 95 * 3 - 1, str_message, 10000)
    # 获取a相相位
    convert_c_phase = deal_645_message(105 * 3, 109 * 3 - 1, str_message, 100)
    # print(convert_c_frequency, convert_c_amplitude, convert_c_phase)

    return transformer_address, collect_time, \
        convert_a_frequency, convert_a_amplitude, convert_a_phase, \
        convert_b_frequency, convert_b_amplitude, convert_b_phase, \
        convert_c_frequency, convert_c_amplitude, convert_c_phase


def parse_message_weisheng_645_calculate(str_message):
    """
    威胜645解析计算数据
    :param str_message:
    :return:
    """
    str_message = " ".join(re.findall(".{2}", str_message))
    # print(str_message)

    # 获取互感器地址
    transformer_address = " ".join(list(reversed(str_message[1 * 3:7 * 3 - 1].split(" ")))).replace(" ", "")
    # print(transformer_address)

    # 获取时间
    str_time = str_message[16 * 3:21 * 3 - 1]
    list_time = [hex(int("0x" + i, 16) - 0x33)[2:].zfill(2) for i in str_time.split(" ")]
    collect_time = "20" + list_time[4] + "-" + list_time[3] + "-" + list_time[2] + " " + list_time[1] + ":" + \
               list_time[0] + ":00"
    # print(collect_time)

    # 获取a相比差
    convert_a_ratio = deal_645_message(21 * 3, 28 * 3 - 1, str_message, 100000000)
    # print(convert_a_ratio)
    # 获取b相比差
    convert_b_ratio = deal_645_message(35 * 3, 42 * 3 - 1, str_message, 100000000)
    # print(convert_b_ratio)
    # 获取c相比差
    convert_c_ratio = deal_645_message(49 * 3, 56 * 3 - 1, str_message, 100000000)
    # print(convert_c_ratio)

    # 获取a相角差
    convert_a_angle = deal_645_message(63 * 3, 70 * 3 - 1, str_message, 100000000)
    # print(convert_a_angle)
    # 获取b相角差
    convert_b_angle = deal_645_message(77 * 3, 84 * 3 - 1, str_message, 100000000)
    # print(convert_b_angle)
    # 获取c相角差
    convert_c_angle = deal_645_message(91 * 3, 98 * 3 - 1, str_message, 100000000)
    # print(convert_c_angle)
    return transformer_address, collect_time, convert_a_ratio, convert_b_ratio, convert_c_ratio, convert_a_angle, \
        convert_b_angle, convert_c_angle


def parse_message_huali_645_sample(str_message):
    """
    华立645解析采样数据
    :param str_message: 
    :return: 
    """
    str_message = " ".join(re.findall(".{2}", str_message))
    # print(str_message)

    # 获取互感器地址
    transformer_address = " ".join(list(reversed(str_message[1 * 3:7 * 3 - 1].split(" ")))).replace(" ", "")
    # print(transformer_address)

    # 获取时间
    str_time = str_message[14 * 3:20 * 3 - 1]
    list_time = [hex(int("0x" + i, 16) - 0x33)[2:].zfill(2) for i in str_time.split(" ")]
    collect_time = "20" + list_time[5] + "-" + list_time[4] + "-" + list_time[3] + " " + list_time[2] + ":" + list_time[
        1] + ":" + list_time[0]
    # print(collect_time)

    # 获取a相频率
    convert_a_frequency = deal_645_message(35 * 3, 41 * 3 - 1, str_message, 10000)
    # 获取b相频率
    convert_b_frequency = deal_645_message(41 * 3, 47 * 3 - 1, str_message, 10000)
    # 获取c相频率
    convert_c_frequency = deal_645_message(47 * 3, 53 * 3 - 1, str_message, 10000)
    # print(convert_a_frequency, convert_b_frequency, convert_c_frequency)

    # 获取a相幅值
    convert_a_amplitude = deal_645_message(53 * 3, 59 * 3 - 1, str_message, 10000)
    # 获取b相幅值
    convert_b_amplitude = deal_645_message(59 * 3, 65 * 3 - 1, str_message, 10000)
    # 获取c相幅值
    convert_c_amplitude = deal_645_message(65 * 3, 71 * 3 - 1, str_message, 10000)
    # print(convert_a_amplitude, convert_b_amplitude, convert_c_amplitude)

    # 获取a相相位
    convert_a_phase = deal_645_message(71 * 3, 76 * 3 - 1, str_message, 100)
    # 获取b相相位
    convert_b_phase = deal_645_message(76 * 3, 81 * 3 - 1, str_message, 100)
    # 获取c相相位
    convert_c_phase = deal_645_message(81 * 3, 86 * 3 - 1, str_message, 100)
    # print(convert_a_phase, convert_b_phase, convert_c_phase)

    return transformer_address, collect_time, \
        convert_a_frequency, convert_a_amplitude, convert_a_phase, \
        convert_b_frequency, convert_b_amplitude, convert_b_phase, \
        convert_c_frequency, convert_c_amplitude, convert_c_phase


def parse_message_huali_645_calculate(str_message):
    """
    华立645解析计算数据
    :param str_message:
    :return:
    """
    str_message = " ".join(re.findall(".{2}", str_message))
    # print(str_message)

    # 获取互感器地址
    transformer_address = " ".join(list(reversed(str_message[1 * 3:7 * 3 - 1].split(" ")))).replace(" ", "")
    # print(transformer_address)

    # 获取时间
    str_time = str_message[14 * 3:20 * 3 - 1]
    list_time = [hex(int("0x" + i, 16) - 0x33)[2:].zfill(2) for i in str_time.split(" ")]
    collect_time = "20" + list_time[5] + "-" + list_time[4] + "-" + list_time[3] + " " + list_time[2] + ":" + list_time[
        1] + ":" + list_time[0]
    # print(collect_time)

    # 获取a相比差
    convert_a_ratio = deal_645_message(35 * 3, 41 * 3 - 1, str_message, 100000000)
    # print(convert_a_ratio)

    # 获取b相比差
    convert_b_ratio = deal_645_message(41 * 3, 47 * 3 - 1, str_message, 100000000)
    # print(convert_b_ratio)

    # 获取c相比差
    convert_c_ratio = deal_645_message(47 * 3, 53 * 3 - 1, str_message, 100000000)
    # print(convert_c_ratio)

    # 获取a相角差
    convert_a_angle = deal_645_message(53 * 3, 59 * 3 - 1, str_message, 100000000)
    # print(convert_a_angle)

    # 获取b相角差
    convert_b_angle = deal_645_message(59 * 3, 65 * 3 - 1, str_message, 100000000)
    # print(convert_b_angle)

    # 获取c相角差
    convert_c_angle = deal_645_message(65 * 3, 71 * 3 - 1, str_message, 100000000)
    # print(convert_c_angle)
    return transformer_address, collect_time, convert_a_ratio, convert_b_ratio, convert_c_ratio, convert_a_angle, \
        convert_b_angle, convert_c_angle


def parse_message_jiangsu_698_sample(str_message):
    """
    解析江苏4G 协议采样
    :param str_message:
    :return:
    """
    str_message = " ".join(re.findall(".{2}", str_message))
    # print(str_message)
    # 获取互感器地址
    transformer_address = " ".join(list(str_message[28 * 3:34 * 3 - 1].split(" ")))
    # 去除字符串空格
    transformer_address = transformer_address.replace(" ", "")
    # print(transformer_address)

    # 获取开始时间
    str_time = str_message[34 * 3:42 * 3 - 1]
    # print(str_time)
    assert int(str_time[:2], 16) == 28, "时间类型非28"
    year = str(int(str_time[3:8].replace(" ", ""), 16))
    month = str(int(str_time[9:12], 16)).zfill(2)
    day = str(int(str_time[12:15], 16)).zfill(2)
    hour = str(int(str_time[15:18], 16)).zfill(2)
    minute = str(int(str_time[18:21], 16)).zfill(2)
    second = str(int(str_time[21:24], 16)).zfill(2)
    collect_time = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second
    # print(collect_time)

    # 获取A相采集频率
    str_frequency_a = str_message[70 * 3:75 * 3 - 1]
    # print(str_frequency_a)
    assert int(str_frequency_a[:2], 16) == 6, "频率类型非6"
    frequency_a = int(str_frequency_a[2:].replace(" ", ""), 16)
    # print(frequency_a)

    # 获取B相采集频率
    str_frequency_b = str_message[75 * 3:80 * 3 - 1]
    # print(str_frequency_b)
    assert int(str_frequency_b[:2], 16) == 6, "频率类型非6"
    frequency_b = int(str_frequency_b[2:].replace(" ", ""), 16)
    # print(frequency_b)

    # 获取C相采集频率
    str_frequency_c = str_message[80 * 3:85 * 3 - 1]
    # print(str_frequency_c)
    assert int(str_frequency_c[:2], 16) == 6, "频率类型非6"
    frequency_c = int(str_frequency_c[2:].replace(" ", ""), 16)
    # print(frequency_c)

    # 获取A相采集幅值
    str_amplitude_a = str_message[85 * 3:94 * 3 - 1]
    # print(str_amplitude)
    assert int(str_amplitude_a[:2], 16) == 21, "幅值类型非21"
    amplitude_a = int(str_amplitude_a[2:].replace(" ", ""), 16)
    # print(amplitude_a)

    # 获取B相采集幅值
    str_amplitude_b = str_message[94 * 3:103 * 3 - 1]
    # print(str_amplitude)
    assert int(str_amplitude_b[:2], 16) == 21, "幅值类型非21"
    amplitude_b = int(str_amplitude_b[2:].replace(" ", ""), 16)
    # print(amplitude_b)

    # 获取C相采集幅值
    str_amplitude_c = str_message[103 * 3:112 * 3 - 1]
    # print(str_amplitude)
    assert int(str_amplitude_c[:2], 16) == 21, "幅值类型非21"
    amplitude_c = int(str_amplitude_c[2:].replace(" ", ""), 16)
    # print(amplitude_c)

    # 获取A相采集相位
    str_phase_a = str_message[112 * 3:117 * 3 - 1]
    # print(str_phase_a)
    assert int(str_phase_a[:2], 16) == 5, "相位类型非5"
    if str_phase_a[3:5] == "FF" or str_phase_a[3:5] == "ff":
        phase_a = int(str_phase_a[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_phase_a[3:5] == "00":
        phase_a = int(str_phase_a[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_phase_a[3:5])
    # print(phase_a)

    # 获取B相采集相位
    str_phase_b = str_message[117 * 3:122 * 3 - 1]
    # print(str_phase_b)
    assert int(str_phase_b[:2], 16) == 5, "相位类型非5"
    if str_phase_b[3:5] == "FF" or str_phase_b[3:5] == "ff":
        phase_b = int(str_phase_b[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_phase_b[3:5] == "00":
        phase_b = int(str_phase_b[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_phase_b[3:5])
    # print(phase_b)

    # 获取C相采集相位
    str_phase_c = str_message[122 * 3:127 * 3 - 1]
    # print(str_phase_c)
    assert int(str_phase_c[:2], 16) == 5, "相位类型非5"
    if str_phase_c[3:5] == "FF" or str_phase_c[3:5] == "ff":
        phase_c = int(str_phase_c[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_phase_c[3:5] == "00":
        phase_c = int(str_phase_c[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_phase_c[3:5])
    # print(phase_c)
    return transformer_address, collect_time, frequency_a, amplitude_a, phase_a, \
        frequency_b, amplitude_b, phase_b, frequency_c, amplitude_c, phase_c


def parse_message_jiangsu_698_calculate(str_message):
    """
    解析江苏4G 698协议计算
    :param str_message:
    :return:
    """
    str_message = " ".join(re.findall(".{2}", str_message))
    # print(str_message)
    # 获取互感器地址
    transformer_address = " ".join(list(reversed(str_message[28 * 3:34 * 3 - 1].split(" "))))
    #  去除字符串空格
    transformer_address = transformer_address.replace(" ", "")
    # print(transformer_address)

    # 获取时间
    str_time = str_message[34 * 3:42 * 3 - 1]
    assert int(str_time[:2], 16) == 28, "时间类型非28"
    year = str(int(str_time[3:8].replace(" ", ""), 16))
    month = str(int(str_time[9:12], 16)).zfill(2)
    day = str(int(str_time[12:15], 16)).zfill(2)
    hour = str(int(str_time[15:18], 16)).zfill(2)
    minute = str(int(str_time[18:21], 16)).zfill(2)
    second = str(int(str_time[21:24], 16)).zfill(2)
    collect_time = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second
    # print(collect_time)

    # 获取A相比差
    str_ratio_a = str_message[70 * 3:79 * 3 - 1]
    # print(str_ratio_a)
    assert int(str_ratio_a[:2], 16) == 20, "比差类型非20"
    if str_ratio_a[3:5] == "FF" or str_ratio_a[3:5] == "ff":
        ratio_a = int(str_ratio_a[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_ratio_a[3:5] == "00":
        ratio_a = int(str_ratio_a[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_ratio_a[3:5])
    # print(ratio_a)

    # 获取B相比差
    str_ratio_b = str_message[79 * 3:88 * 3 - 1]
    # print(str_ratio_b)
    assert int(str_ratio_b[:2], 16) == 20, "比差类型非20"
    if str_ratio_b[3:5] == "FF" or str_ratio_b[3:5] == "ff":
        ratio_b = int(str_ratio_b[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_ratio_b[3:5] == "00":
        ratio_b = int(str_ratio_b[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_ratio_b[3:5])
    # print(ratio_b)

    # 获取C相比差
    str_ratio_c = str_message[88 * 3:97 * 3 - 1]
    # print(str_ratio_c)
    assert int(str_ratio_c[:2], 16) == 20, "比差类型非20"
    if str_ratio_c[3:5] == "FF" or str_ratio_c[3:5] == "ff":
        ratio_c = int(str_ratio_c[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_ratio_c[3:5] == "00":
        ratio_c = int(str_ratio_c[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_ratio_c[3:5])
    # print(ratio_c)

    # 获取A相角差
    str_angle_a = str_message[97 * 3:106 * 3 - 1]
    # print(str_angle)
    assert int(str_angle_a[:2], 16) == 20, "角差类型非20"
    if str_angle_a[3:5] == "FF" or str_angle_a[3:5] == "ff":
        angle_a = int(str_angle_a[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_angle_a[3:5] == "00":
        angle_a = int(str_angle_a[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_angle_a[3:5])
    # print(angle_a)

    # 获取B相角差
    str_angle_b = str_message[106 * 3:115 * 3 - 1]
    # print(str_angle)
    assert int(str_angle_b[:2], 16) == 20, "角差类型非20"
    if str_angle_b[3:5] == "FF" or str_angle_b[3:5] == "ff":
        angle_b = int(str_angle_b[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_angle_b[3:5] == "00":
        angle_b = int(str_angle_b[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_angle_b[3:5])
    # print(angle_b)

    # 获取C相角差
    str_angle_c = str_message[115 * 3:124 * 3 - 1]
    # print(str_angle)
    assert int(str_angle_c[:2], 16) == 20, "角差类型非20"
    if str_angle_c[3:5] == "FF" or str_angle_c[3:5] == "ff":
        angle_c = int(str_angle_c[3:].replace(" ", ""), 16) - int("100000000", 16)
    elif str_angle_c[3:5] == "00":
        angle_c = int(str_angle_c[3:].replace(" ", ""), 16)
    else:
        assert False, "报文错误:{}".format(str_angle_c[3:5])
    # print(angle_c)
    return transformer_address, collect_time, ratio_a, ratio_b, ratio_c, angle_a, angle_b, angle_c


if __name__ == '__main__':
    # str_message = "686500C305010015000043008229850300500202000300202102000020680200002069020001011C07E807050E00000204060007A12015000000000009346B050000005904100000020305000000000500000000041001F5000107E807050F0F0000000082B316"
    str_message = "686500c30502001111111100e84c850300500202000300202102000020680200002069020001011c07e807140d0f000204060007a120150000000000094eae05fff503bd0410000002030500000009050000006904100000000107e807140e0f0f0000009ff516"
    print(len(str_message))
    print("parse_message_xuji_698")
    parse_message_xuji_698(str_message)

    # str_message = "688600C3050100080000330055528503005002020004002021020000206802000020690200002070020001011C07E70519162D000204060007A120150000000000094E670500000063041000000203050000004505000000650410000002061101050000000005000000000500000000050000000004100015000107E7051A000218000000A1EC16"
    # print(len(str_message))
    # print("parse_message_xuji_698_2070")
    # parse_message_xuji_698_2070(str_message)
    #
    # str_message = "68020019000043689D793334375348346356563856BCCC7C333368A68A3333593733333386353333333AB574B344C77BB35434B833CACC7C3333B9A58A3333C349333333373B3333337A873534567386B368CB95B33633833333CAA68A3333433933333356373333335685633335A585B36C44453367333333336833333333333333331D16"
    # print(len(str_message))
    # print("parse_message_weisheng_645_sample")
    # parse_message_weisheng_645_sample(str_message)
    #
    # str_message = "68030016000045689D5F3336375348343334553856B663643B333333973B3333333333A57A483B3333335A3333333333336B78563B33333387363333333333A7AB4C583333333A463333333333CA4BB8333433335C3A33333333336BAC84CC333333933933333333339D16"
    # print(len(str_message))
    # print("parse_message_weisheng_645_calculate")
    # parse_message_weisheng_645_calculate(str_message)
    #
    # str_message = "68CA00C305030055000042109FBA850100228002000102111C07E705191600001C07E705191600000A10303030303030303030303030303030301103060007A120060007A120060007A120150000000000093282150000000000093120150000000000093274050000002F05FFF5036405000AFC700A10303030303030303030303030303030300A10303030303030303030303030303030300A10303030303030303030303030303030300A1030303030303030303030303030303030000107E7051916000000000084B416"
    # print(len(str_message))
    # print("parse_message_huali_698_sample")
    # parse_message_huali_698_sample(str_message)
    #
    # str_message = "68C700C30503005500004210F10D8501002281020001020E1C07E70519090F001C07E70519090F000A103030303030303030303030303030303011031400000000007A3C931400000000007EB28E14000000000079F2CE14000000000610D32814000000000642397D1400000000058DDE870A10303030303030303030303030303030300A10303030303030303030303030303030300A10303030303030303030303030303030300A1030303030303030303030303030303030000107E70519090F00000000935C16"
    # print(len(str_message))
    # print("parse_message_huali_698_calculate")
    # parse_message_huali_698_calculate(str_message)
    #
    # str_message = "6802000800003368916C323434233333395B38563333395B3856343333333333333335353383333333433383333333C5CC7C3333333BB78A33333347AB8A3333337A9B8A33333374983333337A68A433B34897A5333334333333333333333433333333333333343333333333333334333333333333332216"
    # print(len(str_message))
    # print("parse_message_huali_645_sample")
    # parse_message_huali_645_sample(str_message)
    #
    # str_message = "6802000800003368915D323437233333395B38563333395B38563333333333333333359A78CC3A33335B3AC93A333374C4C83A3333645338A83433389334333433AA9769CC333334333333333333333433333333333333343333333333333334333333333333338B16"
    # print(len(str_message))
    # print("parse_message_huali_645_calculate")
    # parse_message_huali_645_calculate(str_message)
